package main

import (
	"fmt"
	"html"
	"log"
	"net/http"
	"os/exec"
)

// 实现一个很简单的gitee webhook回调逻辑，触发devops的操作
func main() {
	// http://zengjian:1165c17f7d8950652fe5d7bd2f747285c2@xxx.com:8080/generic-webhook-trigger/invoke?token=1165c17f7d8950652fe5d7bd2f747285c2
	http.HandleFunc("/generic-webhook-trigger/invoke", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
		shell()
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

// git clone xxxx
// git switch master
// rm -rf /usr/share/nginx/docsify-doc/docs
// mkdir /usr/share/nginx/docsify-doc/docs
// export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64"
// export GROOVY_HOME="/usr/share/groovy-4.0.12"
// export PATH="$PATH:$GROOVY_HOME/bin"
// # groovy ./SidebarGenerator.groovy
// cp -r ./docsify-doc/docs /usr/share/nginx/docsify-doc

func shell(commands ...[]string) {
	cmd := exec.Command("git", "clone", "https://gitee.com/codedesign-fun/code-c-plus.git")
	output, err := cmd.CombinedOutput() // 获取标准输出和标准错误
	if err != nil {
		panic(err)
	}
	fmt.Println(string(output))
}

func exeShellScript() {
	// 将要执行的shell命令
	cmd := "/path/to/your/script.sh"

	// 使用exec.Command执行命令
	output, err := exec.Command("/bin/sh", cmd).CombinedOutput()
	if err != nil {
		fmt.Printf("Execution error: %s\n", err)
		return
	}

	// 打印输出结果
	fmt.Printf("Output: %s\n", output)
}
